var speed : int;
var direction : Vector3; 
var trailPiece : GameObject;
var trail : GameObject;
static var spawn : int = 1;
var instancePos : Vector3;
var i : float = 0;
//Function called when te game starts
function Awake(){
	instancePos = transform.position;
	spawnTrail();
}

function OnTriggerEnter (other : Collider) {
	if (other.gameObject.CompareTag ("Trail")) {
		Debug.Log("explode");	
	}
}

//function that repeats every tick
function Update () {
	
	//position for the instances
	instancePos = (transform.position+Vector3(0,-0.05,0));
	
	//random for gaps in the trail
	spawn = Random.Range(0,1000);
	
	//left key function
	if (Input.GetKey ("left")|| Input.GetKey ("a")){
		direction = Vector3(0,0,80);
	}
	//right key function
	if (Input.GetKey ("right")|| Input.GetKey ("d")){
		direction = Vector3(0,0,-80);
	}
	//default if no key is pressed
	if (!Input.GetKey ("right")& !Input.GetKey ("left")& !Input.GetKey ("d")& !Input.GetKey ("a")){
		direction = Vector3(0,0,0);
	}
	//Movement On key function
	if (Input.GetKey ("i")){
		speed = 20;
	}
	//Movement Off key function
	if (Input.GetKey ("o")){
		speed = 0;
	}
	//apply transformations to bike
	transform.Rotate(direction * Time.deltaTime);
	transform.Translate((direction.up * speed) * Time.deltaTime);
	
}		  

//function that instantiates trail mesh
function spawnTrail(){
	Debug.Log("Start");
	//rotation for the instances
	instanceRot = transform.rotation* Quaternion.Euler(Vector3(90,90,90));
	
	trailInstance = Instantiate( trailPiece, instancePos, instanceRot);
	//Physics.IgnoreCollision( trailPiece. collider, transform.root.collider );
	trailInstance.transform.tag = "PassiveTrail";
	
	i++;
	//stop the invoke
//	CancelInvoke("spawnTrail");
	//recall start function
//	Awake();
}


